﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace David_Albert
{
    public partial class FormCategoria : Form
    {
        //Els necessitem
        DataTable Tcategoria;
        SqlDataAdapter DAdapter;



        public FormCategoria()
        {
            InitializeComponent();
          
            Tcategoria = null;
            DAdapter = null;
            //creo el data Table a partir de la taula categoria de la base de dades
            Tcategoria = new DataTable("Subject");
        }

        private void FormCategoria_Load(object sender, EventArgs e)
        {
            //Assignem el dataTable el grid
            dGVCat.DataSource = Tcategoria;
            //Ompli el dataTable a traves de la BD
            string conStr = ConfigurationManager.ConnectionStrings["MyLibellus"].ConnectionString;
            SqlConnection c = new SqlConnection(conStr);
            c.Open();
            DAdapter = new SqlDataAdapter("Select * FROM Subject", c);
            DAdapter.Fill(Tcategoria);//carrega totes les dades
            SqlCommandBuilder sqlCB = new SqlCommandBuilder(DAdapter);
        }

        private void btnMPCat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDelCat_Click(object sender, EventArgs e)
        {

            DialogResult r = new DialogResult();
            r=MessageBox.Show("Vols eliminar el registre?",
                "Informacio", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                if (dGVCat.SelectedRows.Count > 0)
                {
                    dGVCat.Rows.Remove(dGVCat.SelectedRows[0]);
                    DAdapter.Update(Tcategoria);
                }
                else
                {
                    MessageBox.Show("No hi ha cap categoria seleccionada", "Informació", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {

            }
           
            object s = new object();
            EventArgs l = new EventArgs();
            Tcategoria.Clear();
            FormCategoria_Load(s, l);        
        }

        private void btnAfgCat_Click(object sender, EventArgs e)
        {
            FormAfegirCat frmAc = new FormAfegirCat();
            frmAc.ShowDialog();
            DataRow r = Tcategoria.NewRow();
            if (frmAc.cat.Categoria1 != null)
            {
                r["subject"] = frmAc.cat.Categoria1;
                Tcategoria.Rows.Add(r);
                //Modificar a la BD
                DAdapter.Update(Tcategoria);
            }
            object s = new object();
            EventArgs l = new EventArgs();
            Tcategoria.Clear();
            FormCategoria_Load(s, l);
        }

        private void btnModCat_Click(object sender, EventArgs e)
        {
            if (dGVCat.SelectedRows.Count > 0)
            {
                DataRow s= Tcategoria.NewRow();
                DataGridViewRow r = dGVCat.SelectedRows[0];
                int i = dGVCat.SelectedRows[0].Index;
                Categoria c = new Categoria(int.Parse(dGVCat.SelectedRows[0].Cells[0].Value.ToString()), 
                (dGVCat.SelectedRows[0].Cells[1].Value.ToString()));
                FormModificarCat frmModificarCAt = new FormModificarCat(c);
                frmModificarCAt.ShowDialog();
                s["id"] = frmModificarCAt.a.Id.ToString();
                s["subject"] = frmModificarCAt.a.Categoria1.ToString();
                //PeopleTable.Rows.InsertAt(r, 3);
                Tcategoria.Rows.InsertAt(s, i+1);
                //Modificar a la BD
                DAdapter.Update(Tcategoria);
            }
            else
            {
                MessageBox.Show("No hi ha cap categoria seleccionada", "Informació", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
           
            object p = new object();
            EventArgs l = new EventArgs();
            Tcategoria.Clear();
            FormCategoria_Load(p, l);


  
        }
    }
}
